using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace Pongster
{
    public class GameRoot : Microsoft.Xna.Framework.Game
    {
        public string[] settingsFile;

        GraphicsDeviceManager graphics;
        public SpriteBatch SpriteBatch;

        public RenderTarget2D finalRender; 
        
        GameStateManager stateManager;
        InputHandler inputHandler;
        SoundManager soundManager;

        public TitleScreen TitleScreen;
        public StartMenuScreen StartMenuScreen;
        public OptionsScreen OptionsScreen;
        public GamePlayScreen GamePlayScreen;
        public PopUpScreen PopUpScreen;

        public int ScreenWidth
        {
            get { return Settings.screenWidth; }
        }
        public int ScreenHeight
        {
            get { return Settings.screenHeight; }
        }

        public SoundManager SoundManager
        {
            get { return soundManager; }
        }

        public GameStateManager StateManager
        {
            get { return stateManager; }
        }

        public Rectangle BaseRectangle;

        Texture2D Pointer;

        public GameRoot()
        {
            graphics = new GraphicsDeviceManager(this);
            Resolution.Init(ref graphics);

            if (!File.Exists("settings.data"))
            {
                Settings.SaveSettingsFile();
            }

            settingsFile = File.ReadAllLines("settings.data");

            Settings.Init(
                Convert.ToInt32(settingsFile[0]),
                Convert.ToInt32(settingsFile[1]),
                Convert.ToInt32(settingsFile[2]),
                Convert.ToBoolean(settingsFile[3]),
                Convert.ToInt32(settingsFile[4]),
                Convert.ToInt32(settingsFile[5]),
                Convert.ToInt32(settingsFile[6])
                );

            Resolution.SetVirtualResolution(Settings.basescreenWidth, Settings.basescreenHeight);
            Resolution.SetResolution(Settings.screenWidth, Settings.screenHeight, Settings.screenMode);
            Settings.applyAspectRatio();
                        
            BaseRectangle = new Rectangle(
                  0,
                  0,
                  Settings.basescreenWidth,
                  Settings.basescreenHeight);

            Content.RootDirectory = "Content";

            inputHandler = new InputHandler(this);
            Components.Add(inputHandler);

            soundManager = new SoundManager(this);
            
            stateManager = new GameStateManager(this);
            Components.Add(stateManager);

            TitleScreen = new TitleScreen(this, stateManager);
            StartMenuScreen = new StartMenuScreen(this, stateManager);
            OptionsScreen = new OptionsScreen(this, stateManager);
            GamePlayScreen = new GamePlayScreen(this, stateManager);
            PopUpScreen = new PopUpScreen(this, stateManager);

            //Controls FPS
            //this.IsFixedTimeStep = true;
            //TargetElapsedTime = TimeSpan.FromMilliseconds(17);
            //graphics.SynchronizeWithVerticalRetrace = false;

            stateManager.ChangeState(TitleScreen);
        }


        protected override void Initialize()
        {
            finalRender = new RenderTarget2D(GraphicsDevice, Settings.basescreenWidth, Settings.basescreenHeight); 

            base.Initialize();
        }

        protected override void LoadContent()
        {
            SpriteBatch = new SpriteBatch(GraphicsDevice);

            Pointer = Content.Load<Texture2D>(@"GUI\PointerF");
                        
            //IsMouseVisible = true;
            Window.Title = "Pongster!";
        }


        protected override void UnloadContent()
        {
        }


        protected override void Update(GameTime gameTime)
        {
            //if (Keyboard.GetState().IsKeyDown(Keys.Escape))
            //    this.Exit();

            if (this.IsActive == false)
                inputHandler.Enabled = false;
            else
                inputHandler.Enabled = true;

            Settings.Update(gameTime);
            
            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            //Draw to Render Target
            GraphicsDevice.SetRenderTarget(finalRender);

            GraphicsDevice.Clear(Color.Black);
            
            base.Draw(gameTime);

            SpriteBatch.Begin();

            if (Settings.isCursorVisible == true)
            SpriteBatch.Draw(Pointer, InputHandler.MousePosition, Color.White);   
         
            SpriteBatch.End();

            //Draw to Screen
            GraphicsDevice.SetRenderTarget(null);

            GraphicsDevice.Clear(Color.Black);
            
            SpriteBatch.Begin();

            SpriteBatch.Draw(finalRender, Settings.area, Color.White);

            SpriteBatch.End();

            //SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Resolution.getTransformationMatrix());
            //SpriteBatch.End();
        }

    }
}
